package com.example.android;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class MyImageThread extends Thread{
	private Handler handler;
	private URL u;
	String result = null;
//	public static final String url = "http://www.mengxianyi.net/one/homepage.json";

	public MyImageThread(Handler handler,String string) {
		this.handler = handler;
		try {
			this.u = new URL(string);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		
		try {
//			u = new URL(url);//设置网址
			HttpURLConnection connection = (HttpURLConnection) u.openConnection();//http与url链接
			if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){//connection 的请求码 与HTTP_OK相同 
				InputStream is = connection.getInputStream();//获取输入流
				BufferedReader reader = new BufferedReader(new InputStreamReader(is));//读流
				String line = null;
				
				while((line = reader.readLine())!=null){
					result +=line;
				}
				Log.i("haahaha", result);
				is.close();
				reader.close();
				connection.disconnect();
			}
			
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Message msg = new Message();
		msg.what = 200;
		msg.obj = result;
		handler.sendMessage(msg);

	}
}
